<template>
  <div class="singer" v-loading="loading">
    <index-list
      :singers="singers"
      @selectSingerFn="selectSingerFn"
    ></index-list>
    <router-view
      v-slot="{ Component }"
    >
      <transition
        appear
        name="slide"
      >
        <component
          :is="Component"
          :singer="selectSinger"
        />
      </transition>
    </router-view>
  </div>
</template>

<script lang="ts">
import { defineComponent, onBeforeMount, ref } from 'vue'
import { getSingerList } from '@/service/singer'
import IndexList from '@/components/base/index-list/index.vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { SongProp } from '@/store/type'
export interface SingerItem {
  title: string;
  list: SongProp[]
}
export default defineComponent({
  components: {
    IndexList
  },
  setup () {
    const store = useStore<SongProp>()
    const router = useRouter()
    const singers = ref<SingerItem[]>([])
    const loading = ref<boolean>(false)
    const selectSinger = ref<null | SongProp>(null)
    onBeforeMount(async () => {
      loading.value = true
      const { singers: sig } = await getSingerList()
      singers.value = sig
      loading.value = false
    })
    const selectSingerFn = (singer: SongProp) => {
      selectSinger.value = singer
      router.push({
        path: `/singer/${singer.mid}`
      })
      // 將选择的歌手存进store中
      store.commit('updateSongVal', singer)
    }
    return {
      singers,
      loading,
      selectSingerFn,
      selectSinger
    }
  }
})
</script>

<style lang="scss">
.singer {
  position: fixed;
  width: 100%;
  top: 85px;
  bottom: 0;
  // overflow: scroll;
}
</style>
